﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using SWC=System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Reporting.WinForms;
using System.Configuration;
using System.Windows.Forms;
using AG.Utilities;
using SharePointOptimizer.Model;
using SharePointOptimizer.SQLServer;
using SharePointOptimizer.PL.WPF.Datasets.DataSetSharePointContentIssuesReportTableAdapters;
using SharePointOptimizer.PL.WPF.Datasets;
namespace SharePointOptimizer.PL.WPF
{
    /// <summary>
    /// Interaction logic for IssuesReportControl.xaml
    /// </summary>
    public partial class IssuesReportControl : SWC.UserControl
    {
        public IssuesReportControl()
        {
            InitializeComponent();
        }
        // Global Paremeters for the Issue report ServiceId, Service, severityId, database, databaseId.
        Service instance;
        Database database;
        SharePointOptimizerDataContext dc;
        List<Service> SQLServerInstances;

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (MainWindow.IsDatabaseCreated())
            {
            }
        }        

        private void combobox_Severity_SelectionChanged(object sender, SWC.SelectionChangedEventArgs e)
        {
            try
            {
                RefreshReport();
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }


        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            RefreshReport();
        }


        public void RefreshReport()
        {

            try
            {
                ReportViewer viewer = (ReportViewer)wfh_rptIssues.Child;

                IssuesReportTableAdapter adapter = new IssuesReportTableAdapter();
                adapter.Connection.ConnectionString = ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"];

                DataSetSharePointContentIssuesReport dataset = new DataSetSharePointContentIssuesReport();


                string severity;
                int severityId = -1;

                if (this.combobox_Severity.SelectedIndex != -1)
                {
                    severity = (combobox_Severity.SelectedItem.ToString());

                    if (severity == "High")
                    {

                        severityId = 3;

                    }
                    else if (severity == "Med")
                    {


                        severityId = 2;

                    }

                    else if (severity == "Low")
                    {

                        severityId = 1;

                    }
                    else if (severity == "All")
                    {

                        severityId = -1;

                    }
                }
                else
                {
                    severityId = -1;
                }

                string expand = "";
                int expandLevel = 1;

                if (this.combobox_Expand.SelectedIndex != -1)
                {
                    expand = (combobox_Expand.SelectedItem.ToString());

                    if (expand.ToLower() == "none")
                    {

                        expandLevel = 0;

                    }
                    else if (expand.ToLower() == "level2")
                    {
                        expandLevel = 1;
                    }

                    else if (expand.ToLower() == "issues")
                    {
                        expandLevel = 2;
                    }
                }

                
//                adapter.Fill(dataset.Issues, severityId );
                adapter.Fill(dataset.IssuesReport, severityId);

                BindingSource bindingSource = new BindingSource();

                bindingSource.DataSource = dataset;
                bindingSource.DataMember = "IssuesReport";


                ReportDataSource reportDataSource = new ReportDataSource();
                viewer.Reset();
                reportDataSource.Name = "DataSetSharePointContentIssuesReport";
                reportDataSource.Value = bindingSource;

                ReportParameter param = new ReportParameter("ExpandLevel", expandLevel.ToString());

                viewer.LocalReport.ReportPath = System.Windows.Forms.Application.StartupPath + @"\Reports\SharePointContentIssues.rdlc";
                viewer.LocalReport.DataSources.Add(reportDataSource);
                viewer.LocalReport.EnableHyperlinks = true;
                viewer.BorderStyle = BorderStyle.None;
               viewer.LocalReport.SetParameters(param);
                viewer.RefreshReport();




            }

            catch (Exception ex)
            {
                Logger.Error(ex);
            }





        }

        private void combobox_Expand_SelectionChanged(object sender, SWC.SelectionChangedEventArgs e)
        {
            RefreshReport();
        }

        
        
    }
}
